﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Header :WebControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((!object.Equals(Request.Cookies["RFriend_Email"], null)) && 
            (!object.Equals(Request.Cookies["RFriend_PWD"], null)) && 
            (!object.Equals(Request.Cookies["RFriend_UID"], null)))
        {
            if ((!object.Equals(Request.Cookies["RFriend_Email"].Value, "")) && 
                (!object.Equals(Request.Cookies["RFriend_PWD"].Value, "")) && 
                (!object.Equals(Request.Cookies["RFriend_UID"], "")))
            {
                Session["UserEmail"] = Request.Cookies["RFriend_Email"].Value;
                Session["Password"] = Request.Cookies["RFriend_PWD"].Value;
                Session["UserId"] = Request.Cookies["RFriend_UID"].Value;
            }
        }
        LoadOptions();
    }

    void LoadOptions()
    {
        DBDataContext db = new DBDataContext();
        int? roleId = 0;
        bool pageAllow = false;
        if (Session["User" + Session.SessionID] != null)
        {
            UserQ user = (UserQ)Session["User" + Session.SessionID];
            roleId = user.RoleId;
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

        var pages = db.GetListPagesByRole(roleId);

        List<Module> lstModule = new List<Module>();
        List<String> url = new List<String>();

        foreach (GetListPagesByRoleResult item in pages)
        {
            if (!pageAllow)
            {
                pageAllow = HttpContext.Current.Request.Url.LocalPath.Contains(item.DIRECTION);
            }

            if (item.MODULEID == null)
            {
                Module module = new Module();
                module.Name = item.DESCRIPTION;
                module.ModuleId = item.PAGEID.Value;
                module.Order = item.ORDER;

                url.Add(item.DIRECTION);
                lstModule.Add(module);
            }
            else
            {
                Module module = lstModule.Find(delegate(Module m) { return m.ModuleId == item.MODULEID; });
                if (module != null)
                {
                    PageQ page = new PageQ();
                    page.Description = item.DESCRIPTION;
                    page.Direction = item.DIRECTION;
                    page.Order = item.ORDER;
                    page.PageId = item.PAGEID.Value;
                    module.PageQs.Add(page);
                }
            }
        }

        if (!HttpContext.Current.Request.Url.LocalPath.Contains("Login.aspx") && !HttpContext.Current.Request.Url.LocalPath.Contains("Home.aspx"))
        {
            if (!pageAllow)
            {
                Response.Redirect("Login.aspx");
            }
        }

        int i = 0;

        LIModulePromoter.Visible = false;
        LIPromoter.Visible = false;
        LIPromoterRegister.Visible = false;
        LIModuleUser.Visible = false;
        LIRegister.Visible = false;
        LIChangePassword.Visible = false;
        LIClose.Visible = true;
        LIPromoterSummary.Visible = false;
        LIReport.Visible = false;
        foreach (Module m in lstModule)
        {
            if (m.ModuleId == 100)
            {
                LIModulePromoter.Visible = true;
            }
            if (m.ModuleId == 200)
            {
                LIModuleUser.Visible = true;
            }
            foreach (PageQ p in m.PageQs)
            {
                if (p.PageId == 3)
                {
                    LIRegister.Visible = true;
                }
                if (p.PageId == 4)
                {
                    LIChangePassword.Visible = true;
                }
                if (p.PageId == 1)
                {
                    LIPromoter.Visible = true;
                }
                if (p.PageId == 2)
                {
                    LIPromoterRegister.Visible = true;
                }
                if (p.PageId == 8)
                {
                    LIReport.Visible = true;
                    LIPromoterSummary.Visible = true;
                }
            }
            i++;
        }

        
    }


}
